using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Nemesis;
using Nemesis.WoW.ObjectManager;

namespace Nemesis.CustomClasses
{
    class DefaultShamanElemental : Nemesis.CustomClasses.CCBase
    {
        private WowLocalPlayer Me = ObjectManager.Me;
        private bool AutoMovement = true;

        public override string Name
        {
            get { return "DefaultShamanElemental"; }
        }

        public override void Pull(Nemesis.WoW.ObjectManager.WowUnit Unit)
        {
            CastSpell("Lightning Shield", !Me.HasAura("Lightning Shield"), Me);
            CastSpell("Searing Totem", !UnitIsInRange(2523), Unit);
            CastSpell("Lava Burst", true, Unit);
            CastSpell("Unleash Flame", true, Unit);
            CastSpell("Flame Shock", !Unit.HasAura("Flame Shock"), Unit);
        }


        public override void CombatLoop(Nemesis.WoW.ObjectManager.WowUnit Unit)
        {
            WoW.Classes.Timeout timeout = new WoW.Classes.Timeout(3000);
            while (WoW.Tools.Spells.GlobalCooldown && !timeout.IsTimedOut)
                Thread.Sleep(10);

            //CD
            if (CastSpell("Ascendance", Unit.Elite || Me.GetNumAttackers > 1, Unit)) return;
            if (CastSpell("Elemental Mastery", Unit.Elite || Me.GetNumAttackers > 1, Unit)) return;
            if (CastSpell("Blood Fury", Unit.Elite || Me.GetNumAttackers > 1, Unit)) return;
            //multi target
            while (Me.GetNumAttackers > 2)
            {

                if (CastSpell("Fire Elemental Totem", Me.GetNumAttackers > 2 || Me.HealthPercentage > 70, Unit)) return;
                if (CastSpell("Earth Elemental Totem", Me.GetNumAttackers > 2 || Me.HealthPercentage < 40, Unit)) return;
                if (CastSpell("Blood Lust", Me.GetNumAttackers > 2, Unit)) return;
                if (CastSpell("Chain Lightning", !(Me.HasAura("Ascendance")) && Me.GetNumAttackers > 2, Unit)) return;
                if (CastSpell("Lava Beam", Me.GetNumAttackers > 2, Unit)) return;
                if (CastSpell("Earthquake", Me.GetNumAttackers > 2, Unit)) return;
                //survival CD
                if (CastSpell("Astral Shift", Me.GetNumAttackers > 2 || Me.HealthPercentage < 40, Unit)) return;
                if (CastSpell("Shamanistic Rage", Me.GetNumAttackers > 1 || Me.HealthPercentage < 70, Unit)) return;
            }
            //selfheal
            if (CastSpell("Healing Surge", Me.HealthPercentage < 60, Me)) return;
            //dot
            if (CastSpell("Flame Shock", !(Unit.HasAura("Flame Shock")) || ((Me.AuraStack("Lightning Shield") > 12) && (Unit.Auras.FirstOrDefault(x => x.Name == "Flame Shock").Remaining < 6)) , Unit)) return;
            if (CastSpell("Earth Shock", ((Me.AuraStack("Lightning Shield") > 15)), Unit)) return;
            //single target
            if (CastSpell("Searing Totem", !UnitIsInRange(2523), Unit)) return;
            if (CastSpell("Unleash Flame", true, Unit)) return;
            if (CastSpell("Elemental Blast", true, Unit)) return;
            if (CastSpell("Lava Burst", true, Unit)) return;
            if (CastSpell("Lightning Bolt", (WoW.Tools.Spells.GetWoWSpellOrOverrideByName("Lava Burst").CooldownLeft > 0), Unit)) return;
            //interrupt and slow
            if (CastSpell("Wind Shear", Unit.CanInterruptSpell, Unit)) return;
            if (CastSpell("Frost Shock", Unit.IsFleeing, Unit)) return;
            //survival CD
            if (CastSpell("Astral Shift", Me.GetNumAttackers > 2 || Me.HealthPercentage < 40, Unit)) return;
            if (CastSpell("Shamanistic Rage", Me.GetNumAttackers > 1 || Me.HealthPercentage < 70, Unit)) return;
        }

        public override void PostCombat()
        {
            while (Me.HealthPercentage < 80)
                CastSpell("Healing Surge", true, Me);
        }

        public override void Patrolling()
        {
        }

        public bool UnitIsInRange(uint unitId)
        {
            List<Nemesis.WoW.ObjectManager.WowObject> list = new List<WowObject>(Nemesis.WoW.ObjectManager.ObjectManager.GetUnitsAroundPosition(Me.Position, 25));
            bool match = list.Any(t => t.Entry == unitId);
            if (match)
                return true;
            else
                return false;
        }

        public bool CastSpell(string spellname, bool condition, WoW.ObjectManager.WowUnit targetUnit)
        {
            if (!condition)
                return false;

            WoW.Classes.WowSpell wowSpell = WoW.Tools.Spells.GetWoWSpellOrOverrideByName(spellname);

            if (!wowSpell.IsValid || !wowSpell.IsUsable)
                return false;

            if (AutoMovement)
            {
                int minRange = (int)wowSpell.MinRange;
                int maxRange = (int)wowSpell.MaxRange;

                // Maybe move back here
                if (minRange > 0 && targetUnit.DistanceToMe < minRange)
                    return false;

                // Move forward in Range
                if (maxRange > 0 && targetUnit.DistanceToMe > maxRange)
                    WoW.Actions.Approach.Unit(targetUnit, maxRange - 1, true);
            }


            if (targetUnit.IsLocalPlayer)
                WoW.Actions.SpellManager.CastSpellByID(wowSpell.SpellId, true, false);
            else
            {
                if (Me.Target.BaseAddress != targetUnit.BaseAddress)
                    targetUnit.SetTarget();

                WoW.Actions.SpellManager.CastSpellByID(wowSpell.SpellId, targetUnit, false);
            }
            return false;
        }

        #region unused
        public override void Custom1() { }
        public override void Custom2() { }
        public override void Custom3() { }
        public override void Custom4() { }
        public override void Custom5() { }
        public override void Custom6() { }
        public override void Custom7() { }
        public override void Custom8() { }
        public override void Custom9() { }
        public override void Custom10() { }
        #endregion
    }
}
